package com.ss.warfarin.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ss.warfarin.persistence.model.User;
import com.ss.warfarin.service.LoginService;

public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
//		PrintWriter out = response.getWriter();

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		LoginService loginService = new LoginService();
		User user = loginService.checkLogin(username, password);
		
		if (user != null) {
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/jsp/history.jsp");
			requestDispatcher.forward(request, response);

		} else {
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("login.jsp");
			request.setAttribute("msgIncorrect", "Incorrect Username or Password !!!");
			requestDispatcher.forward(request, response);
		}
	}

}